<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>notify-rule-receive-add</title>
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/form.jsp"%>
	<%@ include file="../include/tree.jsp"%>
	 <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jsframework.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/Union/dateUtil.js"></script>
    <script src="<%=request.getContextPath()%>/js/calendar/zh_CN_WdatePicker.js"></script>
	<script src="${pageContext.request.contextPath}/js/crontab/Simple_util.js"></script>
	<script src="<%=request.getContextPath()%>/js/crontab/Crontab.js"></script>
	<script src="<%=request.getContextPath()%>/js/crontab/Crontab_data_zh_CN.js"></script>
    <script src="${pageContext.request.contextPath}/js/TimeUnit/TimeUnit.js"></script>
    <script src="${pageContext.request.contextPath}/js/TimeUnit/TimeUnit_data_zh_CN.js"></script>
   <!-- <script type="text/javascript" src="${pageContext.request.contextPath}/js/widgets/floatChooseLayer.js"></script> --> 
	<style type="text/css">
		.tdValue{
			border: 1px solid #D6D3D6;
			line-height: 26px;
			height: 32px;
			padding-left: 5px;
			overflow: hidden;
		}
		.ztree {border: 1px solid #617775;background: #f0f6e4;overflow-y:scroll;overflow-x:auto;}
	</style>
	<script type="text/javascript">
		/*var stu;
		new FloatChooseLayer({
			tTargets : ["times"],
			options : [1,2,3,4,5,6,7,8,9,10,20,50,100],
			CustomMethods : {
				spanClickAfter : function (){
					dochange();
				}
			}
		});*/
		 function dochange(){
		        var countTime = $("#times").val();
		        var reg = /^[0-9]+$/;
		        if(!reg.test(countTime)){
		        	return;
		        }
		        if(countTime > 1){
		        	$("#vailTime").css({"display":""});
		        	$("#treeDemo").css({"height":"200px"});
		        }else{
		        	$("#vailTime").css({"display":"none"});
		        	$("#treeDemo").css({"height":"260px"});
		        }
		        //clearSBS().byId(stu.getTU().DomIds().day);
		   }
	  function add(){
		  var duratime = stu.results();
		  if($("#times").val() != '1'){
			  if(duratime == '0'){
				  getTopWin(window).getMessageBox().alert("时间间隔不能为空","提示");
			  	  return false;
			  }
		  }else{
			  duratime = 0;
		  }
		  if($("#user").val() == ''){
			  getTopWin(window).getMessageBox().alert("请选择用户","提示");
		  	  return false;
		  }
			$.ajax({
					type: "POST", 
					url: "${pageContext.request.contextPath}/notify/notify-receive-rule!save.action?interval="+duratime, 
					data: $("#myform").serialize(), //传的参数
					dataType: 'json',
					success: function(data){ //结果
						   	if(data.flag == true){
						   	 var pWin = getTopWin(window).currentWin["add-receive-rule"];
							   pWin.datagrid();
							   getTopWin(window).closejBox("add-receive-rule");
						   	}else if(data.flag == false){
						   		getTopWin(window).getMessageBox().alert("所添加的用户和通知方式包含已经存在的","提示");
						   	}else if(data.del == true){
						   		getTopWin(window).getMessageBox().alert("此条通知接收者记录已经被删除，请返回列表并刷新","提示");
						   	}
					   }
			});
	  }
	$(function(){
		stu = new SimpleTimeUnit("interval");
		stu.show();
		showMenu();
		$.formValidator.initConfig({formid:"myform"
	          ,onerror:function(msg){}
	          ,onsuccess:function(){
	        	  		add();
	          			return false;
	          }
	    });
		 $("#times").formValidator({onshow:"次数(1-99) 恢复事件只通知一次",onfocus:"次数(1-99) 恢复事件只通知一次",oncorrect:" "}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:2,onerror:"长度非法"})
		 .functionValidator({
			    fun:function(val,elem){
			    	 var reg = /^[0-9]+$/;
			    	    if(!reg.test(val)){
			    	        return "必须为整数";
			    	    }
			    	    return true;
				}
	  });
	})
	var setting = {
			check: {
				enable: true,
				chkboxType: {"Y" : "ps", "N" : "ps"}
			},
			view: {
				dblClickExpand: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onCheck: onCheck
			}
		};
		function onCheck(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getCheckedNodes(true),
			m = "";
			for (var i=0, l=nodes.length; i<l; i++) {
				//过滤出部门
				if(!(nodes[i].id.indexOf(',') > 0)){
					m += nodes[i].id + ",";
				}
			}
			if (m.length > 0 ) m = m.substring(0, m.length-1);
			$("#user").val(m);
		}
		function showMenu() {
			$.ajax({ 
				  type:"post", 
				  url:"${pageContext.request.contextPath}/event/notify-his!userTreeData.action?userId="+$('#user').val(), 
				  data:"", 
				  datatype:"json",
				  success:function(data) { 
						var zNodes=eval(data);
						$.fn.zTree.init($("#treeDemo"), setting, zNodes);
				  } 
		     	})
		}
	function closewin(){
	  getTopWin(window).closejBox("add-receive-rule");
  }
  function pageReset(){
	  $("#myform").get()[0].reset(); 
	  stu = new SimpleTimeUnit("interval");
		stu.show();
		$("#vailTime").css({"display":"none"});
	}
	</script>
  </head>
  
  <body class="padding-8">
  	<div onmousemove="dochange();">
  		<form action="" id="myform" name="myform" method="post">
    		<table class="table_eidt">
    			<tr style="height: 50px">
	    			<td class="td_left" width="100px">通知方式</td>
	    			<td class="td_right">
					<select name="notifyMode" id="notifyMode" style="width: 150px">
			          <option value="1">短信</option>
			          <option value="2">邮件</option>
			        </select>
	    			<span id="ipTip" style="width:150px"></span></td>
    			</tr>
    			<tr style="height: 50px">
	    			<td class="td_left"><span class="span_alert">*</span>通知次数</td>
	    			<td class="td_right">
	    				<input type="text" name="times" id="times" maxlength="5" value="1"/>
	    				<span id="timesTip" style="width:150px"></span>
	    			</td>
    			</tr>
    			<tr style="display: none; height: 50px"  id="vailTime">
	    			<td class="td_left"><span class="span_alert">*</span>时间间隔</td>
	    			<td class="tdValue">
	    				<div id="interval"></div>
	    			</td>
    			</tr>
    			<tr style="height: 50px">
	    			<td class="td_left"><span class="span_alert">*</span>接收人</td>
	    			<td class="td_right">
	    			<div id="menuContent">
						<ul id="treeDemo" class="ztree" style="margin-top:0; width:340px; height: 260px;"></ul>
					</div>
					<input id="user" type="hidden" name="user"/>
	    			<span id="userNameTip" style="width:150px"></span></td>
    			</tr>
    		</table>
    		<div align="center" style="margin-top:5px;">
	    		<input type="submit" id="button" name="button" value="保存" class="btn-4">
	    		<input type="button" class="btn-4" onclick="pageReset();" value="重置">
	    		<input type="button" value="取消" onclick="closewin();" class="btn-4">
    		</div>
    		<input type="hidden" id="notifyRuleId" name="notifyRuleId" value=${notifyRuleId}>
    	</form>
  	</div>
  </body>
</html>
